<template>
  <el-dialog :title="title+'部门'" :visible.sync="showDialog" @close="close">
    <el-form ref="form" label-width="120px" :model="formData" :rules="formRules">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="formData.name" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" placeholder="请选择负责人" style="width: 80%" size="mini">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          placeholder="1-100个字符"
          type="textarea"
          size="mini"
          :rows="4"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="btnOk">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { getDepartmentListApi, getDepartmentManagerApi, addDepartmentApi, getDepartmentInfoApi, editDepartmentApi } from '@/api/department'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    parentId: {
      type: [Number, String],
      default: ''
    }

  },
  data() {
    return {
      formData: {
        'name': '',
        'code': '',
        'managerId': '',
        'introduce': ''

      },

      formRules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2-10个字符', trigger: 'blur' },
          {
            trigger: 'blur', validator: async(rule, value, callback) => {
              const res = await getDepartmentListApi()
              // res.filter(item => item.name === value).length ? callback(new Error('部门名称已经存在')) : callback
              if (this.formData.id) {
                res.filter(item => item.id !== this.formData.id && item.name === value).length ? callback(new Error('部门名称已经存在')) : callback
              } else {
                res.filter(item => item.name === value).length ? callback(new Error('部门名称已经存在')) : callback
              }
            }
          }
        ],
        code: [{ required: true, message: '请输入', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2-10个字符', trigger: 'blur' },
          {
            trigger: 'blur', validator: async(rule, value, callback) => {
              const res = await getDepartmentListApi()
              // res.filter(item => item.code === value).length ? callback(new Error('部门编码已经存在')) : callback
              if (this.formData.id) {
                res.filter(item => item.id !== this.formData.id && item.code === value).length ? callback(new Error('部门编码已经存在')) : callback
              } else {
                res.filter(item => item.code === value).length ? callback(new Error('部门编码已经存在')) : callback
              }
            }
          }],
        managerId: [{ required: true, message: '请输入', trigger: 'blur' }],
        introduce: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      managerList: {}
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '新增'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    close() {
      this.formData = {
        'name': '',
        'code': '',
        'managerId': '',
        'introduce': ''
      }
      this.$refs.form.resetFields()
      this.$refs.form.clearValidate()
      this.$emit('update:showDialog', false)
    },
    async getManagerList() {
      const res = await getDepartmentManagerApi()
      // console.log(res)
      this.managerList = res
    },
    async btnOk() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        await editDepartmentApi(this.formData)
      } else {
        const formData = { ...this.formData, pid: this.parentId }

        await addDepartmentApi(formData)
      }

      this.$message.success('操作成功')
      this.close()
      // 通知列表更新子传父
      this.$emit('updateList')
    },
    async reviewFormData(id) {
      const res = await getDepartmentInfoApi(id)
      this.formData = res
    }

  }
}
</script>
